package top.mcwebsite.playandroid.business.articleDetail.impl.vm

import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import top.mcwebsite.playandroid.common.webview.WebViewAction
import top.mcwebsite.playandroid.lib.mvi.base.BaseViewModel

class ArticleDetailViewModel : BaseViewModel<ArticleDetailIntent, ArticleDetailUiState, ArticleDetailUiEffect>(
    defaultState = ArticleDetailUiState()
) {

    private lateinit var url: String

    fun init(url: String) {
        this.url = url
    }

    private val webViewActionMutableSharedFlow = MutableSharedFlow<WebViewAction>()

    val webViewActionFlow = webViewActionMutableSharedFlow.asSharedFlow()

    override fun handleIntent(intent: ArticleDetailIntent) {
        when (intent) {
            is ArticleDetailIntent.ClickBackIntent -> {
                sendUiEffect(ArticleDetailUiEffect.NavigationBackUiEffect)
            }

            is ArticleDetailIntent.ClickMoreIntent -> {
                setUiState { copy(showShareBottomSheet = true) }
            }

            is ArticleDetailIntent.DismissBottomSheetIntent -> {
                setUiState { copy(showShareBottomSheet = false) }
            }
            is ArticleDetailIntent.ClickOperationItemIntent -> {
                setUiState { copy(showShareBottomSheet = false) }
                handleOperationIntent(intent.type)
            }
            is ArticleDetailIntent.FindInputChangeIntent -> {
                setUiState { copy(findKey = intent.input) }
            }
            is ArticleDetailIntent.FindIntent -> {
                find()
            }
            is ArticleDetailIntent.FindPreIntent -> {
                viewModelScope.launch { webViewActionMutableSharedFlow.emit(WebViewAction.FindPre) }
            }
            is ArticleDetailIntent.FindNextIntent -> {
                viewModelScope.launch { webViewActionMutableSharedFlow.emit(WebViewAction.FindNext) }
            }
            is ArticleDetailIntent.FindCloseIntent -> {
                viewModelScope.launch { webViewActionMutableSharedFlow.emit(WebViewAction.FindClose) }
                setUiState { copy(findKey = "", showFindInPage = false) }
            }
        }
    }

    private fun find() {
        viewModelScope.launch {
            webViewActionMutableSharedFlow.emit(WebViewAction.Find(uiState.findKey))
        }
    }

    private fun handleOperationIntent(operationType: OperationType) {
        when (operationType) {
            OperationType.CopyLink -> {
                sendUiEffect(ArticleDetailUiEffect.CopyUrlToClipboardUiEffect(url))
            }
            OperationType.Refresh -> {
                viewModelScope.launch { webViewActionMutableSharedFlow.emit(WebViewAction.Refresh) }
            }
            OperationType.OpenInBrowser -> {
                sendUiEffect(ArticleDetailUiEffect.OpenInDefaultBrowser(url))
            }
            OperationType.Find -> {
                setUiState { copy(showFindInPage = true) }
//                viewModelScope.launch { webViewActionMutableSharedFlow.emit(WebViewAction.Find("a")) }
            }
        }
    }
}